$(function(){

    //点击发货的按钮出现弹框
    $('.delivery').click(function (){
        $('.mask-change').show();
        $('.delivery_tankuang').show();
        getExpressCompany( 5 );
    });

    // 从后台获取快递公司....需要用到ajax请求接口...
    function getExpressCompany( type )
    {
        $.ajax({
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:finddictlist,
            dataType:'json',
            data:{
                type:type
            },
            success:function(data){
                if(data.resultCode == 200)
                {
                    var value = data.value;
                    $('.expressCompany').html('');
                    $('.expressCompany').append('<option value="">请选择快递公司</option>');
                    for( var i=0;i<value.length;i++ )
                    {
                        $('.expressCompany').append('<option value="'+value[i].id+'" description="'+value[i].description+'">'+value[i].name+'</option>');
                    };
                }
                else if(data.resultCode == 515)
                {
                    reLogin();
                }
                else
                {
                    console.log('接口出错!非200,非515!')
                };
                // console.log(data)
                console.log('获取标签列表接口....请求成功!!!')
            },
            error:function(){
                console.log('获取标签列表接口....请求失败...')
            },
        });
    };
    // console.log( JSON.parse(base.decode(localStorage.getItem( base.encode('userinfo') )) ).id   )
// console.log( JSON.parse( base.decode( localStorage.getItem( localStorage.getItem( base.encode('userinfo') ) ) ) ) )
    //点击确定发货...跟后台做交互...
    $('.sure_delivery').click(function(){

        var expressNum = $('input[name = expressNum]').val();
        var expressCompanyId  = $('.expressCompany option:selected').val();
        var expressCompanyName = $('.expressCompany option:selected').html();
        var expressCompanyNameSimple = $('.expressCompany option:selected').attr('description');

        // 首先要验证必填项输入不能为空...
        if( expressNum == '' || expressCompanyId == '' )
        {
            alert('必填项不能为空!!')
        }
        else
        {
            $.ajax({
                type:'post',
                headers: {"Accept-CMS-Token":token},
                url:sendout,
                dataType:'json',
                data:{
                    userid:JSON.parse(base.decode(localStorage.getItem( base.encode('userinfo') )) ).id,
                    expressSendName:JSON.parse(base.decode(localStorage.getItem( base.encode('userinfo') )) ).name,
                    expressCompanyId:expressCompanyId,
                    expressCompanyName:expressCompanyName,
                    expressCompanyNameSimple:	expressCompanyNameSimple,
                    expressNum:expressNum,
                    orderId:$.getUrlParam('orderId'),
                    type:0
                },
                success:function(data){
                    if(data.resultCode == 200)
                    {
                        alert('亲,发货成功!');
                        window.location.href = ready_delivery;

                    }
                    else if(data.resultCode == 515)
                    {
                        reLogin();
                    }
                    else
                    {
                        console.log('接口出错!非200,非515!')
                    };
                    console.log(data)
                    console.log('获取标签列表接口....请求成功!!!')
                },
                error:function(){
                    console.log('获取标签列表接口....请求失败...')
                },
            });

        };

    });

    // 页面初始化...执行获取接口数据的函数....
    getOrderDetail($.getUrlParam('orderId'));

    // 根据订单的id来查询订单的详情.....向后台发送请求....获取接口当中的数据..
    function getOrderDetail(orderId)
    {
        $.ajax({
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:orderdetails,
            dataType:'json',
            data:{
                orderId:orderId
            },
            success:successFn,
            error:function(){
                console.log('加载部门列表接口请求失败')
            },
        });
    };

    // 跟后台ajax请求接口....成功的时候就执行下面的这个函数...
    function successFn(data)
    {
        if( data.resultCode == 200 )
        {
            $('.orderStatus').html( changeOrderStatus( data.value.order.orderStatus ) );//订单状态

            if( data.value.order.isRefound == 0 )
            {//没有申请退款...按钮可以正常使用
                $('.send_goods input').attr('disabled',false);
            }
            else if( data.value.order.isRefound == 1 )
            {//有申请退款...按钮是禁用状态
                $('.send_goods input').attr('disabled',true);
            };

            $('span.receiveName').html(  data.value.order.receiveName  );//买家信息
            $('span.receivePhone').html(  data.value.order.receivePhone  );//联系电话
            $('span.receiveAddress').html(  data.value.order.receiveAddress  );//收货地址
            // 展示商品信息
            clearDetailTable('.look_waitGoods_detail');
            var list = data.value.list;
            for( var i = 0;i<list.length;i++ )
            {
                $('.look_waitGoods_detail').append('<tr></tr>');
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(i+1)+'</td>');
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(list[i].ShopCommodity.name)+'</td>');
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(data.value.order.orderNum)+'</td>');
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(list[i].ShopCommodity.colorName)+'</td>');
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(list[i].ShopCommodity.modelName)+'</td>');
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(list[i].ShopCommodity.sizeName)+'</td>');
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(list[i].ShopCommodityOrder.amount)+'</td>');
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(list[i].ShopCommodityOrder.prices)+'</td>');//单价
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+(list[i].ShopCommodityOrder.prices)*(list[i].ShopCommodityOrder.amount)+'</td>');//总价
                $('.look_waitGoods_detail tr').eq(i+1).append('<td>'+changeNormalGoods(list[i].ShopCommodityOrder.status)+'</td>');//订单状态
            };
            //展示订单信息
            $('span.totalMoney').html( data.value.order.totalMoney );//订单总金额
            $('span.expressMoney').html( data.value.order.expressMoney );//运费
            $('span.fundsReceived').html( data.value.order.totalMoney + data.value.order.expressMoney );//实收款
            $('span.orderNum').html( data.value.order.orderNum );//订单编号
            $('span.payOrderNum').html( data.value.order.payOrderNum );//支付交易号
            $('span.createTime').html( getMoth(data.value.order.createTime) );//生成时间
            $('span.payTime').html( getMoth(data.value.order.payTime) );//支付时间

            // console.log(data);
        }
        else if( data.resultCode == 515 )
        {
            reLogin();
        }
        else
        {
            console.log('接口出错!非200,非515!')
        };
        console.log('查询订单详情接口调通!!!!')
    };





});